package stat;

public class DoubleStat {

	private int nOfInterval;

	private double[] value;

	private int[] dist;

	public DoubleStat(int nOfItv, double[] v) {
		nOfInterval = nOfItv;
		value = v;

		init();
	}

	public int getNOfInterval() {
		return nOfInterval;
	}

	public void setNOfInterval(int ofInterval) {
		nOfInterval = ofInterval;
	}

	public double[] getValue() {
		return value;
	}

	public void setValue(double[] value) {
		this.value = value;
	}

	private void init() {
		double min = min(getValue());
		double max = max(getValue());
		double gap = (max - min) / (getNOfInterval() - 1);
		dist = new int[getNOfInterval()];
		for (int i = 0; i < dist.length; i++) {
			dist[i] = 0;
		}

		for (int i = 0; i < getValue().length; i++) {
			int index = (int) Math.floor((getValue()[i] - min) / gap);
//			System.out.println("index : " + index);
			dist[index] += 1;
		}

		double[] pre = new double[dist.length];
		for (int i = 0; i < pre.length; i++) {
			pre[i] = (i + 0.5) * gap;
			System.out.println(pre[i] + "\t" + dist[i]);
		}
	}

	public static double min(double[] v) {
		double min = Double.MAX_VALUE;
		for (int i = 0; i < v.length; i++) {
			if (min > v[i])
				min = v[i];
		}
		return min;
	}

	public static double max(double[] v) {
		double max = Double.MIN_VALUE;
		for (int i = 0; i < v.length; i++) {
			if (max < v[i])
				max = v[i];
		}
		return max;
	}

	public int[] getDist() {
		return dist;
	}

	public static void print(double[] ds) {
		System.out.print("[");
		for (double d : ds) {
			System.out.print(" " + d);
		}
		System.out.print(" ]\n");
	}

}
